#include "linuxheader.h"
#include <workflow/WFHttpServer.h>
static WFFacilities::WaitGroup waitGrop(1);
void sighander(int num)
{

    fprintf(stderr,"waitGrop is done\n");
}

struct SeriesContext
{
    WFHttpTask*serverTask;
};
void httpCallback(WFHttpTask* httpTask)
{
  auto req = httpTask ->get_req();
  auto resp = httpTask->get_resp();
  int state = httpTask->get_state();
  int error = httpTask->get_error();

switch(state)
{
  case WFT_STATE_SYS_ERROR:
  fprintf(stderr,"sys error:%s\n",strerror(error));
  break;
 case WFT_STATE_DNS_ERROR:
  fprintf(stderr,"DNS error:%s\n",gai_strerror(error));
  break;
  case WFT_STATE_SUCCESS:
  break;
}
 if(state!=WFT_STATE_SUCCESS)
 {
    fprintf(stderr,"httpTask is faild\n");
    return ;
 }

else
{
    fprintf(stderr,"success\n");
}
const void *body;
size_t size;
resp->get_parsed_body(&body,&size);
SeriesContext *context = static_cast<SeriesContext*>(series_of(httpTask)->get_context());
auto  resp2client = context->serverTask->get_resp();
*resp2client = std::move(*resp);

}



void process(WFHttpTask*serverTask)
{
   //需要解析来的指令
 auto req = serverTask->get_req();
 auto resp = serverTask->get_resp();
 //需要断言来的内容;如果有连接的话workflow内部已经完成接受以太网帧的信息，可以通过内置的api的接口来获取相关的信息
std::string requestUri = req->get_request_uri();//获取请求路径的信息
//这里可以开始断句（目前觉得是获取密码的但是这个是没有配置密钥的）;
// std::string query= requestUri.substr(requestUri.find("?")+1);
// std::string nameKv = query.substr(0,query.find("&"));
// std::string keyKv =query.substr(query.find("&")+1);
// std::string name = nameKv.substr(nameKv.find("=")+1);
// std::string key = keyKv.substr(keyKv.find("=")+1);
// fprintf(stderr,"name:%s key:%s\n",name.c_str(),key.c_str());
//这里已经获取完毕了
//开始创建任务并且把任务放到serverTask的序列当中；
 
 auto HttpTask =WFTaskFactory::create_http_task("http://47.94.147.94",0,0,httpCallback);
 series_of(serverTask)->push_back(HttpTask);
 SeriesContext *context = new SeriesContext;
context->serverTask = serverTask;
serverTask->set_callback([](WFHttpTask *serverTask){
    fprintf(stderr,"server task callback\n");
    
});
series_of(serverTask)->set_callback([](const SeriesWork *series){
    fprintf(stderr,"series callback\n");
    SeriesContext *context = static_cast<SeriesContext*>(series->get_context());
});

}
int main()
{
    //创建一个服务端
WFHttpServer server(process);
if(server.start(1234)==0)
{
    waitGrop.wait();
    server.stop();

}
else
{
    perror("server start faild\n");
    return -1;
}
    return 0;
}

